package xin.clips.web.controller.gy;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.clips.common.annotation.Log;
import xin.clips.common.enums.BusinessType;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.utils.DictUtils;
import xin.clips.common.utils.poi.ExcelUtil;
import xin.clips.common.core.page.TableDataInfo;
import xin.clips.system.domain.gy.GyDeviceMgt;
import xin.clips.system.service.ISysUserService;
import xin.clips.system.service.gy.IGyDeviceMgtService;

/**
 * 设备消息绑定Controller
 *
 * @author yuanzy
 * @date 2023-02-26
 */
@Controller
@RequestMapping("/gy/mgt")
public class GyDeviceMgtController extends BaseController {
    private String prefix = "gy/mgt";

    @Autowired
    private IGyDeviceMgtService gyDeviceMgtService;
    @Autowired
    private ISysUserService userService;

    @RequiresPermissions("gy:mgt:view")
    @GetMapping()
    public String mgt() {
        return prefix + "/mgt";
    }

    @RequiresPermissions("gy:mgt:add")
    @GetMapping("/getDeptUser")
    public String getDeptUser() {
        return prefix + "/deptUser";
    }


    /**
     * 查询设备消息绑定列表
     */
    @RequiresPermissions("gy:mgt:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GyDeviceMgt gyDeviceMgt) {
        startPage();
        List<GyDeviceMgt> list = gyDeviceMgtService.selectGyDeviceMgtList(gyDeviceMgt);
        return getDataTable(list);
    }

    /**
     * 导出设备消息绑定列表
     */
    @RequiresPermissions("gy:mgt:export")
    @Log(title = "设备消息绑定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GyDeviceMgt gyDeviceMgt) {
        List<GyDeviceMgt> list = gyDeviceMgtService.selectGyDeviceMgtList(gyDeviceMgt);
        ExcelUtil<GyDeviceMgt> util = new ExcelUtil<GyDeviceMgt>(GyDeviceMgt.class);
        return util.exportExcel(list, "设备消息绑定数据");
    }

    /**
     * 新增设备消息绑定
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存设备消息绑定
     */
    @RequiresPermissions("gy:mgt:add")
    @Log(title = "设备消息绑定", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GyDeviceMgt gyDeviceMgt) {
        if (gyDeviceMgt.getIsPrincipal() == null) {
            gyDeviceMgt.setIsPrincipal(0);
        }
        List<GyDeviceMgt> list = gyDeviceMgtService.selectGyDeviceMgtByDeviceType(gyDeviceMgt.getDeviceType());
        if (CollUtil.isEmpty(list)) {
            gyDeviceMgt.setIsPrincipal(1);//设置为责任人
        }
        if (gyDeviceMgt.getIsPrincipal() == 1) {
            if (CollUtil.isNotEmpty(list)) {
                for (int i = 0; i < list.size(); i++) {
                    GyDeviceMgt temp = list.get(i);
                    temp.setIsPrincipal(0);
                    gyDeviceMgtService.updateGyDeviceMgt(temp);
                }
            }
        }

        return toAjax(gyDeviceMgtService.insertGyDeviceMgt(gyDeviceMgt));
    }

    /**
     * 修改设备消息绑定
     */
    @RequiresPermissions("gy:mgt:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GyDeviceMgt gyDeviceMgt = gyDeviceMgtService.selectGyDeviceMgtById(id);
        mmap.put("gyDeviceMgt", gyDeviceMgt);
        return prefix + "/edit";
    }


    /**
     * 修改保存设备消息绑定
     */
    @RequiresPermissions("gy:mgt:edit")
    @Log(title = "设备消息绑定", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GyDeviceMgt gyDeviceMgt) {

        if (gyDeviceMgt.getIsPrincipal() == null) {
            gyDeviceMgt.setIsPrincipal(0);
        }
        List<GyDeviceMgt> list = gyDeviceMgtService.selectGyDeviceMgtByDeviceType(gyDeviceMgt.getDeviceType());
        if (CollUtil.isEmpty(list)) {
            gyDeviceMgt.setIsPrincipal(1);//设置为责任人
        }
        if (gyDeviceMgt.getIsPrincipal() == 1) {
            if (CollUtil.isNotEmpty(list)) {
                for (int i = 0; i < list.size(); i++) {
                    GyDeviceMgt temp = list.get(i);
                    temp.setIsPrincipal(0);
                    gyDeviceMgtService.updateGyDeviceMgt(temp);
                }
            }
        }

        return toAjax(gyDeviceMgtService.updateGyDeviceMgt(gyDeviceMgt));
    }

    /**
     * 修改保存设备消息绑定
     */
    @RequiresPermissions("gy:mgt:edit")
    @PostMapping("/setPrinicipal")
    @ResponseBody
    public AjaxResult setPrinicipal(GyDeviceMgt gyDeviceMgt) {
        GyDeviceMgt dbMgt = gyDeviceMgtService.selectGyDeviceMgtById(gyDeviceMgt.getId());
        List<GyDeviceMgt> list = gyDeviceMgtService.selectGyDeviceMgtByDeviceType(dbMgt.getDeviceType());

        if (CollUtil.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                GyDeviceMgt temp = list.get(i);
                temp.setIsPrincipal(0);
                gyDeviceMgtService.updateGyDeviceMgt(temp);
            }
        }


        dbMgt.setIsPrincipal(1);
        return toAjax(gyDeviceMgtService.updateGyDeviceMgt(dbMgt));
    }

    /**
     * 修改保存设备消息绑定
     */
    @RequiresPermissions("gy:mgt:edit")
    @PostMapping("/setRelate")
    @ResponseBody
    public AjaxResult setRelate(GyDeviceMgt gyDeviceMgt) {
        GyDeviceMgt dbMgt = gyDeviceMgtService.selectGyDeviceMgtById(gyDeviceMgt.getId());
        dbMgt.setIsPrincipal(0);
        return toAjax(gyDeviceMgtService.updateGyDeviceMgt(dbMgt));
    }


    /**
     * 修改保存设备消息绑定
     */
    @RequiresPermissions("gy:mgt:edit")
    @PostMapping("/exists")
    @ResponseBody
    public AjaxResult exists(GyDeviceMgt gyDeviceMgt) {
        List<GyDeviceMgt> mgts = gyDeviceMgtService.selectGyDeviceMgtList(gyDeviceMgt);
        if (CollUtil.isNotEmpty(mgts)) {
            return AjaxResult.error("该用户已经绑定" + DictUtils.getDictLabel("gy_device_type", String.valueOf(gyDeviceMgt.getDeviceType())) + "类型消息！");
        }
        return AjaxResult.success(userService.selectUserById(gyDeviceMgt.getUserId()));
    }

    /**
     * 删除设备消息绑定
     */
    @RequiresPermissions("gy:mgt:remove")
    @Log(title = "设备消息绑定", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(gyDeviceMgtService.deleteGyDeviceMgtByIds(ids));
    }
}
